package com.mhkj.base.messageRemind.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.mhkj.base.busiType.service.BusinessTypeService;
import com.mhkj.base.messageRemind.entity.MessageRemind;
import com.mhkj.base.messageRemind.repository.MessageRemindRepository;
import com.mhkj.base.messageRemind.service.MessageRemindService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;

/**
 * 消息提醒
 * @author wk
 * @date 2018年8月7日
 * @version 1.0
 */
@Service
public class MessageRemindServiceImpl extends BaseServiceImpl<MessageRemind> implements MessageRemindService{
	
	@Resource
	private MessageRemindRepository remindRepository;
	@Resource
	private BusinessTypeService businessTypeService;
	
	@Resource
	public void setMessageRemindRepository(MessageRemindRepository remindRepository) {
		super.setBaseRepository(remindRepository);
	}

	@Override
	public Pagination<MessageRemind> listByPage(MessageRemind messageRemind,
			int pageIndex, int size) {
		Specification<MessageRemind> spec = new Specification<MessageRemind> () {  
			/**
			 * 序列号
			 */
			private static final long serialVersionUID = -786003485941679959L;
			@Override
			public Predicate toPredicate(Root<MessageRemind> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
              List<Predicate> lstPredicates = new ArrayList<Predicate>();
            
              Predicate[] arrayPredicates = new Predicate[lstPredicates.size() ];
              return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
	        }
		};
		Page<MessageRemind> pageT =  remindRepository.findAll(spec, PageRequest.of(pageIndex, size, Sort.Direction.ASC,"id"));
		Pagination<MessageRemind> pagination = new Pagination<MessageRemind>();
		pagination.setTotal(remindRepository.count(spec));
		List<MessageRemind> workCalenders = pageT.getContent();
		pagination.setData(workCalenders);
		return pagination;
	}
}
